﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace LibFriesland_Campina_CRM
{
    class DalSupervisor
    {
        //Lay Supervisor theo ID
        public static BizSupervisor getSupbyID(string pSup_charid)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var sup = db.Supervisor.Single(p => p.SupCharID == pSup_charid);
                return new BizSupervisor(sup.SupCharID, sup.Name, sup.Phone, sup.Email);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        //Lay danh sach Supervisor
        public static List<BizSupervisor> getAll()
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var supervisors = from sup in db.Supervisor
                                  where sup.SupCharID != "SUPDEFAULT" && sup.SupCharID != "suptest"
                                  select new BizSupervisor(sup.SupCharID, sup.Name, sup.Phone, sup.Email);
                return supervisors.ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        #region Dung cho Webservice
        //Lay danh sach NA cua Supervisor
        public static List<BizNA> getNAbySup(string pSup_charid)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            var nas = from na in db.NA
                      where na.BySup == pSup_charid
                      select new BizNA(na.NACharID, Thuvien.ConvertToUnSign(na.Name));
            return nas.ToList();
        }
        #endregion
    }
}
